<template>
	<view :style="{backgroundImage: background,paddingBottom:paddingBottom + 'px'}" class="titleAndHeader">
		<view class="header"
			:style="{height:(navInfo.height)  + 'px',paddingBottom:navInfo.top + 'px',paddingTop:navInfo.top + 'px'}">
			<view :style="{display:hidden ? 'none':'block',marginLeft:'20rpx'}">
				<u-icon @click="go" v-if="pages.length > 1" name="arrow-left" size="24"></u-icon>
				<u-icon @click="go" v-else name="home" size="24"></u-icon>
			</view>
			<text v-if="!(slots.default)" :style="{color:titleColor}" class="title">{{title}}</text>
			<slot v-else></slot>
		</view>
	</view>
</template>

<script setup>
	import {
		reactive,
		ref,
		useSlots
	} from 'vue';
	defineProps({
		title: {
			type: String,
			default: 'uni-app'
		},
		hidden: {
			type: Boolean,
			default: false
		},
		background: {
			type: String,
			default: 'linear-gradient(180deg, #4c8eef 0%, #84bbd900 100%);'
		},
		paddingBottom: {
			type: Number,
			default: 0
		},
		titleColor: {
			type: String,
			default: '#000'
		}
	})
	// 获取导航栏高度
	const navInfo = reactive(wx.getMenuButtonBoundingClientRect())
	// 获取当前页面栈
	const pages = getCurrentPages();
	// 左侧导航
	const go = () => {
		if (pages.length > 1) {
			uni.navigateBack()
		} else {
			uni.switchTab({
				url: '/pages/shouye/shouye'
			})
		}
	}

	// 判断父组件是否传 slot
	const slots = useSlots()
</script>

<style lang="scss">
	.titleAndHeader {
		background-image: linear-gradient(180deg, #4c8eef 0%, #84bbd900 100%);
		background-size: 100% 100%;
	}

	.header {
		box-sizing: content-box;
		display: flex;
		align-items: center;
		position: relative;

		.title {
			position: absolute;
			left: 50%;
			top: 50%;
			transform: translate(-50%, -50%);
			font-size: 18px;
			font-weight: 400;
			letter-spacing: 0px;
			line-height: 26px;
			color: rgba(51, 51, 51, 1);
		}
	}
</style>